package com.example.demoitext;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
 * 创建Pdf文档
 * @author Administrator
 *
 */
public class HelloPdf
{
	private static final Integer TABLE_WIDTH = 560;
	private static final Integer TABLE_CELL_HEIGHT = 30;
    public static void main(String[] args)throws Exception
    {
    	BaseFont bfChinese = BaseFont.createFont("/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
        Font secondFont = new Font(bfChinese, 14, Font.NORMAL);
        Font secondUnderlineFont = new Font(bfChinese, 14, Font.UNDERLINE,  
                BaseColor.BLACK);
        Font firstBlodFont = new Font(bfChinese, 16, Font.BOLD,  
                BaseColor.BLACK);
        Font secondBlodFont = new Font(bfChinese, 14, Font.BOLD,  
                BaseColor.BLACK);
        // 第一步，创建document对象
        Rectangle rectPageSize = new Rectangle(PageSize.A4);
         
        //下面代码设置页面横置
        //rectPageSize = rectPageSize.rotate();
         
        //创建document对象并指定边距
        Document document = new Document(rectPageSize,20,20,10,10);
        // Document document = new Document();
        try
        {
            // 第二步,将Document实例和文件输出流用PdfWriter类绑定在一起
            //从而完成向Document写，即写入PDF文档
            PdfWriter.getInstance(document,new FileOutputStream("D:/Develop/HelloWorld.pdf"));
            //第3步,打开文档
            document.open();
            //第3步,向文档添加文字. 文档由段组成
            Paragraph paragraph = new Paragraph("上海昊沧保养报告",firstBlodFont);
            paragraph.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph);
            document.add(new Paragraph(" ",secondFont));
            PdfPTable table = new PdfPTable(3);
            /*for(int i=0;i<12;i++)
            {
                if (i == 0)
                {
                    PdfPCell cell = new PdfPCell();
                    cell.setColspan(3);
                    cell.setBackgroundColor(new BaseColor(180,180,180));
                    cell.addElement(new Paragraph("表格头" , FontChinese));
                    cell.setBorderWidth(0f);
                    table.addCell(cell);
                }
                else
                {
                    PdfPCell cell = new PdfPCell();
                    cell.addElement(new Paragraph("表格内容" , FontChinese));
                    cell.setBorderWidth(0f);
                    table.addCell(cell);
                }
            }
            document.add(table);*/
            
            List<String> dataList = new LinkedList<>();
            dataList.add("1@编号 : 2018-11");
            dataList.add("1@位置 : 百联南方购物中心,百联南方购物中心");
            dataList.add("1@服务时间 : 2018-11-21");
            dataList.add("1@紧急呼叫 : 1231232131");
            dataList.add("1@维保时间 : 15分33秒");
            dataList.add("1@维保人 : 1231232131");
            // dataList.add("3@维保周期 : 月检");
            
            table = new PdfPTable(3);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            /*int[] relativeWidths = {30,60,30};
            table.setWidths(relativeWidths);*/
            table.setTotalWidth(TABLE_WIDTH);
            table.setLockedWidth(true);
            for(int i=0;i<dataList.size();i++)
            {
                /*if (i == 0)
                {
                    PdfPCell cell = new PdfPCell();
                    cell.setColspan(3);
                    cell.setBackgroundColor(new BaseColor(180,180,180));
                    cell.addElement(new Paragraph("表格头" , FontChinese));
                    cell.setBorderWidth(0f);
                    table.addCell(cell);
                }
                else
                {
                    PdfPCell cell = new PdfPCell();
                    cell.addElement(new Paragraph("表格内容" , FontChinese));
                    cell.setBorderWidth(0f);
                    table.addCell(cell);
                }*/
                PdfPCell cell = new PdfPCell();
                String[] datas = dataList.get(i).split("@");
                cell.setColspan(Integer.parseInt(datas[0]));
                // cell.setBackgroundColor(new BaseColor(180,180,180));
                cell.addElement(new Paragraph(datas[1] , secondFont));
                // removeBorderWidth(cell);
                cell.setBorderWidth(0f);
                table.addCell(cell);
            }
            Object image1 = null;
            URL resource1 = HelloPdf.class.getClassLoader().getResource("2.png");
            System.out.println("url : "+resource1);
            image1 = Image.getInstance(resource1);
            Image newImage1 = (Image)image1;
            newImage1.scaleAbsolute(16, 16);
            // newImage1.setAlignment(Element.ALIGN_CENTER);
            
            PdfPCell cell = new PdfPCell();
            Paragraph p = new Paragraph();
            p.add(new Chunk(newImage1,0,-3));
            p.add(new Chunk(" 年检    ", secondFont));
            p.add(new Chunk(newImage1,0,-3));
            p.add(new Chunk(" 季检    ", secondFont));
            p.add(new Chunk(newImage1,0,-3));
            p.add(new Chunk(" 月检", secondFont));
            removeBorderWidth(cell);
            cell.setColspan(3);
            cell.addElement(p);
            table.addCell(cell);
            document.add(table);
            document.add(new Paragraph(" ",secondFont));
            document.add(new Paragraph("第一部分:检查系统",firstBlodFont));
            document.add(new Paragraph(" ",secondFont));
            document.add(new Paragraph("防火卷帘门系统",secondBlodFont));
            // String imagePath ="classpath:click.png";
            Object image = null;
            URL resource = HelloPdf.class.getClassLoader().getResource("2.png");
            System.out.println("url : "+resource);
            image = Image.getInstance(resource);
            Image newImage = (Image)image;
            newImage.scaleAbsolute(16, 16);
            newImage.setAlignment(Element.ALIGN_CENTER);
            // newImage.setAbsolutePosition(10, 20);
            table = new PdfPTable(5);
            int[] relativeWidths = {35,10,10,10,35};
            table.setWidths(relativeWidths);
            // table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setTotalWidth(TABLE_WIDTH);
            table.setLockedWidth(true);
            String content = "防火卷帘门系统检查";
            createTableFirstRow(table, content,secondFont);
            // PdfPCell cell = null;
            for(int i=0;i<3;i++) {
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	        	cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
            }
            document.add(table);
            
            table = new PdfPTable(5);
            table.setWidths(relativeWidths);
            // table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setTotalWidth(TABLE_WIDTH);
            table.setLockedWidth(true);
            createTableFirstRow(table, "电气控制系统检查", secondFont);
            for(int i=0;i<3;i++) {
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	        	cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
            }
            document.add(table);
            
            table = new PdfPTable(5);
            table.setWidths(relativeWidths);
            // table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setTotalWidth(TABLE_WIDTH);
            table.setLockedWidth(true);
            createTableFirstRow(table, "电气控制系统检查", secondFont);
            for(int i=0;i<3;i++) {
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	        	cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
            }
            document.add(table);
            
            table = new PdfPTable(5);
            table.setWidths(relativeWidths);
            // table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setTotalWidth(TABLE_WIDTH);
            table.setLockedWidth(true);
            createTableFirstRow(table, "电气控制系统检查", secondFont);
            for(int i=0;i<3;i++) {
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	        	cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
            }
            document.add(table);
            
            table = new PdfPTable(5);
            table.setWidths(relativeWidths);
            // table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setTotalWidth(TABLE_WIDTH);
            table.setLockedWidth(true);
            createTableFirstRow(table, "电气控制系统检查", secondFont);
            for(int i=0;i<3;i++) {
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	        	cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(newImage);
	            table.addCell(cell);
	            
	            cell = new PdfPCell();
	            removeBorderWidth(cell);
	            cell.addElement(new Paragraph("1.运行时有无异常震动",secondFont));
	            table.addCell(cell);
            }
            document.add(table);
            
            // 创建结尾签名
            document.add(new Paragraph(" ",secondFont));
            List<String> footerList = new LinkedList<>();
            footerList.add("维保单位报告人 ");
            footerList.add("物业管理确认人 ");
            footerList.add("    甲方确认人 ");
            for(int i=0;i<footerList.size();i++) {
            	document.add(new Paragraph(" ",secondFont));
                p = new Paragraph();
                p.setAlignment(Element.ALIGN_CENTER);
                p.add(new Chunk(footerList.get(i), secondFont));
                p.add(new Chunk("              ", secondUnderlineFont));
                p.add(new Chunk("        ", secondFont));
                p.add(new Chunk("日期 ", secondFont));
                p.add(new Chunk("              ", secondUnderlineFont));
                document.add(p);
            }
            System.out.println("生成HelloPdf成功！");
        }
        catch (DocumentException de)
        {
            System.err.println(de.getMessage());
        }
        catch (IOException ioe)
        {
            System.err.println(ioe.getMessage());
        }
        //关闭document
        document.close();
     }
    
    /**
     * 
     * @Description: 创建表格第一行
     * @param table
     * @param content
     * @param secondFont
     */
    private static void createTableFirstRow(PdfPTable table, String content, Font secondFont) {
    	PdfPCell cell = new PdfPCell();
    	removeBorderWidth(cell);
    	cell.addElement(new Paragraph("★"+content,secondFont));
    	table.addCell(cell);
    	
    	cell = new PdfPCell();
    	removeBorderWidth(cell);
    	Paragraph paragraph = new Paragraph("良好",secondFont);
    	paragraph.setAlignment(Element.ALIGN_CENTER);
    	cell.addElement(paragraph);
    	table.addCell(cell);
    	
    	cell = new PdfPCell();
    	removeBorderWidth(cell);
    	paragraph = new Paragraph("损坏",secondFont);
    	paragraph.setAlignment(Element.ALIGN_CENTER);
    	cell.addElement(paragraph);
    	table.addCell(cell);
    	
    	cell = new PdfPCell();
    	removeBorderWidth(cell);
    	paragraph = new Paragraph("没有",secondFont);
    	paragraph.setAlignment(Element.ALIGN_CENTER);
    	cell.addElement(paragraph);
    	table.addCell(cell);
    	
    	cell = new PdfPCell();
    	removeBorderWidth(cell);
    	cell.addElement(new Paragraph("所需修理工作",secondFont));
    	table.addCell(cell);
	}

	/**
     * 
     * @Description: 去边框
     * @param cell
     */
    public static void removeBorderWidth(PdfPCell cell) {
    	cell.setFixedHeight(TABLE_CELL_HEIGHT);
    	cell.setBorderWidth(0f);
    	cell.setUseAscender(true);
    	cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
    }
}